package com.ynny.chapter07three.dao;

import com.ynny.chapter07three.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl implements UserDao{

    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public int insertUser(User user) {
        String sql = "insert into user(number, password, id) values ( ?, ?, ?)";
        int i = jdbcTemplate.update(sql,
                user.getNumber(),
                user.getPassword(),
                user.getId());
        return i;
    }
    @Override
    public User selectUserByUserName(String userName) {

        String sql = "select * from user where number = ?";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class)
                , userName);

        if (list.size() == 0){
            return null;
        }

        return list.get(0);
    }
}
